package diploma.kisel.modelas.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Domain class for Workstation (table - workstation).
 * 
 * @author Nikolay Kisel
 *
 */
@Entity
@Table(name = "workstation")
@NamedQueries({
		@NamedQuery(name = "Workstation.findAll", query = "select w from Workstation w"),
		@NamedQuery(name = "Workstation.findById", query = "select w from Workstation w where w.id = :id"),
		@NamedQuery(name = "Workstation.findByConcept", query = "select w from Workstation w where w.concept = :concept")})
public class Workstation {

	private Integer id;
	private String number;
	private WorkstationType workstationType;
	private Integer countWorkplaces;
	private Integer countDevices;
	private Integer countServers;

	// for many to one mapping
	private Concept concept;
	// for many to many mapping
	private Set<Device> devices = new HashSet<Device>();
	private Set<Computer> computers = new HashSet<Computer>();
	private Set<Workplace> workplaces = new HashSet<Workplace>();
	private Set<Database> commonDatabases = new HashSet<Database>();

	public Workstation() {

	}

	public Workstation(String number, WorkstationType workstationType) {
		this.number = number;
		this.workstationType = workstationType;
	}	

	public Workstation(String number, WorkstationType workstationType,
			Integer countWorkplaces, Integer countDevices, Integer countServers) {
		this.number = number;
		this.workstationType = workstationType;
		this.countWorkplaces = countWorkplaces;
		this.countDevices = countDevices;
		this.countServers = countServers;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id_workstation")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "number")
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Column(name = "type")
	@Enumerated(EnumType.STRING)
	public WorkstationType getWorkstationType() {
		return workstationType;
	}

	public void setWorkstationType(WorkstationType workstationType) {
		this.workstationType = workstationType;
	}

	@Column(name = "count_wp")
	public Integer getCountWorkplaces() {
		return countWorkplaces;
	}

	public void setCountWorkplaces(Integer countWorkplaces) {
		this.countWorkplaces = countWorkplaces;
	}

	@Column(name = "count_devices")
	public Integer getCountDevices() {
		return countDevices;
	}

	public void setCountDevices(Integer countDevices) {
		this.countDevices = countDevices;
	}

	@Column(name = "count_servers")
	public Integer getCountServers() {
		return countServers;
	}

	public void setCountServers(Integer countServers) {
		this.countServers = countServers;
	}

	@ManyToOne
	@JoinColumn(name = "id_concept")
	public Concept getConcept() {
		return concept;
	}

	public void setConcept(Concept concept) {
		this.concept = concept;
	}

	@ManyToMany
	@JoinTable(name = "list_device", joinColumns = @JoinColumn(name = "id_workstation"), inverseJoinColumns = @JoinColumn(name = "id_device"))
	public Set<Device> getDevices() {
		return devices;
	}

	public void setDevices(Set<Device> devices) {
		this.devices = devices;
	}

	@ManyToMany
	@JoinTable(name = "list_pc", joinColumns = @JoinColumn(name = "id_workstation"), inverseJoinColumns = @JoinColumn(name = "id_pc"))
	public Set<Computer> getComputers() {
		return computers;
	}

	public void setComputers(Set<Computer> computers) {
		this.computers = computers;
	}

	@ManyToMany
	@JoinTable(name = "list_workplace", joinColumns = @JoinColumn(name = "id_workstation"), inverseJoinColumns = @JoinColumn(name = "id_workplace"))
	public Set<Workplace> getWorkplaces() {
		return workplaces;
	}

	public void setWorkplaces(Set<Workplace> workplaces) {
		this.workplaces = workplaces;
	}	

	@ManyToMany
	@JoinTable(name = "list_cdb", joinColumns = @JoinColumn(name = "id_workstation"), inverseJoinColumns = @JoinColumn(name = "id_database"))
	public Set<Database> getCommonDatabases() {
		return commonDatabases;
	}

	public void setCommonDatabases(Set<Database> commonDatabases) {
		this.commonDatabases = commonDatabases;
	}

	@Override
	public String toString() {
		return "Workstation [id=" + id + ", number=" + number
				+ ", workstationType=" + workstationType + ", countWorkplaces="
				+ countWorkplaces + ", countDevices=" + countDevices
				+ ", countServers=" + countServers + ", concept=" + concept
				+ "]";
	}

}
